<?php

/**
 * This is the extended model class for table "cargo_ref".
 */
class cargo_courier_ref extends cargo_ref
{
	/**
	 * The followings are the available columns in table 'cargo_ref':
	 * @var integer $id
	 * @var integer $cargo_id
	 * @var integer $date
	 * @var string $ref_no
	 * @var string $ref_type
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return cargo_ref the static model class
	 */
	public $type = array('DHL'=>'DHL','UPS'=>'UPS','Fedex'=>'Fedex','TNT'=>'TNT','EMS'=>'EMS','Air post'=>'Air post','MAWB/OBL'=>'MAWB/OBL','HAWB/HBL'=>'HAWB/HBL','Permit'=>'Permit','Other'=>'Other');
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cargo_id, date', 'numerical', 'integerOnly'=>true),
			array('ref_no', 'length', 'max'=>25),
			array('ref_type', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, cargo_id, date, ref_no, ref_type', 'safe', 'on'=>'search'),
		);
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ref_no' => 'Courier Ref',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('date',$this->date);

		$criteria->compare('ref_no',$this->ref_no,true);

		$criteria->compare('ref_type','courier:',true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function afterFind(){
		$this->ref_type = str_replace('courier:','',$this->ref_type);
		return parent::afterFind();
	}
	
	public function save(){
		$this->ref_type = "courier:".$this->ref_type;
		return parent::save();
	}
	public function deleteAll($condition,$params=array()){
		$condition.=" AND ref_type like 'courier:%'";
		return parent::deleteAll($condition,$params);
	}
	public function defaultScope()
    {
        return array(
           	'condition'=>"ref_type like 'courier:%'",
        );
    }
}